package com.example.tests;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.example.utils.SortedListOf;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static com.example.tests.ContactDataGenerator.loadContactsFromXmlFile;
//import static com.example.tests.ContactDataGenerator.loadContactsFromCsvFile;

public class ContactCreationTests extends TestBase {

  @DataProvider
  public Iterator<Object[]> contactsFromFile() throws IOException {
	 return wrapContactsForDataProvider(loadContactsFromXmlFile(new File ("contacts.xml"))).iterator();
	 //return wrapContactsForDataProvider(loadContactsFromCsvFile(new File ("contacts.txt"))).iterator();
  }
	
  @Test(dataProvider = /*"contactsFromFile"*/ "randomValidContactGenerator")
  public void testContactCreationWithValidData(ContactData contact) throws Exception {
        
    // gets contact list from created model. Initially it gets contacts from DB,
	// Then model's updated during contact creating, deleting, modification 
	  SortedListOf<ContactData> contactListFromModel = app.getContactHelper().getContactsFromModel(); 
	  
	// get Old contact list from DB (cache)
	  SortedListOf<ContactData> oldContactListFromDB = app.getContactHelper().getContactsFromCache();
    
    // perform actions
    app.getContactHelper().createContact(contact);
        
    // save new state
    SortedListOf<ContactData> newContactListFromUI = app.getContactHelper().getContacts();
    
    // get New contact list from DB (cache)
 	  SortedListOf<ContactData> newContactListFromDB = app.getContactHelper().getContactsFromCache();
    
    // compare states   
 	// compare UI to Model 
    assertThat(newContactListFromUI, equalTo(contactListFromModel));
    
    // compare DB to Model
    assertThat(newContactListFromDB, equalTo(contactListFromModel));
    
    // compare UI and Cached Contact list from DB
    assertThat(newContactListFromUI, equalTo((newContactListFromDB)));
    
    // compare New and Old Contact lists from DB
    assertThat(newContactListFromDB, equalTo((oldContactListFromDB).withAdded(contact)));
  }
 }
